home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte10 / setsclin.c < prev    next >
C/C++ Source or Header  |  1996-01-29  |  8KB  |  282 lines

  1.  
  2. #include <windows.h>  
  3. #include "SetSclIn.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. VOID Scroll( HWND hWnd, int* pnCurPos, WORD wScroll )
  108. {
  109.    SCROLLINFO si;
  110.  
  111.    // Use GetScrollInfo() to get information
  112.    // about the scroll bar.
  113.    //.......................................
  114.    si.cbSize = sizeof( SCROLLINFO );
  115.    si.fMask  = SIF_PAGE | SIF_RANGE | SIF_POS;
  116.    GetScrollInfo( hWnd, SB_VERT, &si );
  117.  
  118.    switch( wScroll )
  119.    {
  120.       case SB_LINEDOWN :
  121.               if ( *pnCurPos <= (int)(si.nMax - si.nPage) )
  122.                  *pnCurPos += 1;
  123.               break;
  124.  
  125.       case SB_LINEUP :
  126.               if ( *pnCurPos > 0 )
  127.                  *pnCurPos -= 1;
  128.               break;
  129.    }
  130.  
  131.    if ( si.nPos != *pnCurPos )
  132.    {
  133.       RECT cltRect;
  134.  
  135.       GetClientRect( hWnd, &cltRect );
  136.  
  137.       ScrollWindowEx( hWnd, 0, (si.nPos-*pnCurPos)*20, NULL,
  138.                      &cltRect, NULL, NULL, SW_INVALIDATE );
  139.  
  140.       si.fMask = SIF_POS;
  141.       si.nPos  = *pnCurPos;
  142.       SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  143.    }
  144. }
  145.  
  146.  
  147. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  148. {
  149. static int  nDspLines;
  150. static int  nNumItems = 0;
  151. static int  nCurPos   = 0;
  152. static char szBuf[10];
  153.  
  154.    switch( uMsg )
  155.    {
  156.       case WM_CREATE :
  157.               ShowScrollBar( hWnd, SB_VERT, TRUE );
  158.               break;
  159.  
  160.          // Every time the window is sized, re-calculate the number of lines
  161.          // the client area can display and set the scroll bar accordingly.
  162.          //................................................................
  163.       case WM_SIZE :
  164.               {
  165.                  RECT rect;
  166.                  GetClientRect( hWnd, &rect );
  167.  
  168.                  nDspLines = rect.bottom / 20;
  169.  
  170.                  if ( nDspLines < nNumItems ) 
  171.                  {
  172.                     SCROLLINFO si;
  173.  
  174.                     si.cbSize = sizeof( SCROLLINFO );
  175.                     si.fMask  = SIF_POS | SIF_RANGE | SIF_PAGE;
  176.                     si.nMin   = 0;
  177.                     si.nMax   = nNumItems-1;
  178.                     si.nPage  = nDspLines;
  179.                     si.nPos   = nCurPos;
  180.  
  181.                     EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );
  182.                     SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  183.                  }
  184.                  else
  185.                     EnableScrollBar( hWnd, SB_VERT, ESB_DISABLE_BOTH );
  186.               }
  187.               break;
  188.                
  189.       case WM_PAINT :
  190.               {
  191.                  PAINTSTRUCT ps;
  192.                  int         i,j;
  193.                  int         nNumPaint;
  194.  
  195.                  nNumPaint = min( nCurPos+nDspLines, nNumItems );
  196.  
  197.                  BeginPaint( hWnd, &ps );
  198.  
  199.                  for ( j=0,i=nCurPos; i<nNumPaint; i++,j++ )
  200.                  {
  201.                     itoa( i, szBuf, 10 );
  202.                     TextOut( ps.hdc, 10, j*20, "Line of Text:", 13 );
  203.                     TextOut( ps.hdc, 90, j*20, szBuf, strlen( szBuf ) );
  204.                  }
  205.                   
  206.                  EndPaint( hWnd, &ps );
  207.               }
  208.               break;
  209.  
  210.       case WM_VSCROLL :
  211.               Scroll( hWnd, &nCurPos, LOWORD( wParam ) );
  212.               break;
  213.  
  214.       case WM_COMMAND :
  215.               switch( LOWORD( wParam ) )
  216.               {
  217.                  case IDM_TEST :
  218.                         if ( nDspLines == nNumItems ) 
  219.                            EnableScrollBar( hWnd, SB_VERT, ESB_ENABLE_BOTH );
  220.  
  221.                         nNumItems++;
  222.                         if ( nDspLines < nNumItems )
  223.                         {
  224.                            SCROLLINFO si;
  225.  
  226.                            si.cbSize = sizeof( SCROLLINFO );
  227.                            si.fMask  = SIF_RANGE | SIF_PAGE;
  228.                            si.nMin   = 0;
  229.                            si.nMax   = nNumItems-1;
  230.                            si.nPage  = nDspLines;
  231.  
  232.                            SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  233.                         }
  234.  
  235.                         InvalidateRect( hWnd, NULL, FALSE );
  236.                         break;
  237.  
  238.                  case IDM_ABOUT :
  239.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  240.                         break;
  241.  
  242.                  case IDM_EXIT :
  243.                         DestroyWindow( hWnd );
  244.                         break;
  245.               }
  246.               break;
  247.       
  248.       case WM_DESTROY :
  249.               PostQuitMessage(0);
  250.               break;
  251.  
  252.       default :
  253.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  254.    }
  255.  
  256.    return( 0L );
  257. }
  258.  
  259.  
  260. LRESULT CALLBACK About( HWND hDlg,           
  261.                         UINT message,        
  262.                         WPARAM wParam,       
  263.                         LPARAM lParam)
  264. {
  265.    switch (message) 
  266.    {
  267.        case WM_INITDIALOG: 
  268.                return (TRUE);
  269.  
  270.        case WM_COMMAND:                              
  271.                if (   LOWORD(wParam) == IDOK         
  272.                    || LOWORD(wParam) == IDCANCEL)    
  273.                {
  274.                        EndDialog(hDlg, TRUE);        
  275.                        return (TRUE);
  276.                }
  277.                break;
  278.    }
  279.  
  280.    return (FALSE); 
  281. }
  282.